<?php session_start();?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录 - 知聊 BBS论坛</title>
    <link rel="stylesheet" href="static/login.css">
</head>
<body>
<?php include './logincheck.php';yeslogin(); ?>

<div class="login-container">
    <h2>欢迎登录</h2>

    <form method="POST" action="">
        <div class="form-group">
            <input type="text" id="username" name="username" placeholder="请输入用户名" required autofocus>
        </div>

        <div class="form-group">
            <input type="password" id="password" name="password" placeholder="请输入密码" required>
        </div>

        <button type="submit">立即登录</button>
    </form>

    <div class="footer-links">
        <a href="register.php">注册账号</a>
    </div>
</div>
</body>
</html>
<?php
if (!empty($_POST['username']) and !empty($_POST['password'])){
    $username= $_POST['username'];
    $password = $_POST['password'];
    include './conn.php';
    $sql = "select * from users where username = ? and password = ?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("ss", $username, $password);
    $stmt->execute();
    $result = $stmt->get_result();
    
    // 获取sql语句执行的信息
    if ($result->num_rows > 0) {
        // 输出数据并将数据给到_SESSION
        $_SESSION = $result->fetch_assoc();;
        header('Location: index.php');

        $conn->close();


    } else {
        $conn->close();
        die('<script>alert("账号密码错误！")</script>');
    }
}